package com.ts.content.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ts.common.core.utils.Result;
import com.ts.content.constants.RedisKeys;
import com.ts.content.entity.DiscussEntity;
import com.ts.content.service.DiscussService;
import io.swagger.annotations.Api;
import org.apache.commons.lang.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;


/**
 * 话题
 *
 * @author admin admin@qq.com
 * @since 1.0.0 2023-02-22
 */
@RestController
@RequestMapping("/discuss")
@Api(tags="话题")
public class DiscussController {
    @Autowired
    private DiscussService discussService;

    @Autowired
    private StringRedisTemplate redisTemplate;

    /**以下是提供远程调用的*/
    //查询话题的分页信息
    @PostMapping("/list")
    public Page<DiscussEntity> list(@RequestBody Page<DiscussEntity> page ){
        return discussService.page(page);
    }

    @PostMapping("/save")
    public Result<DiscussEntity> save(@RequestBody DiscussEntity discussEntity){
        discussService.save(discussEntity);
        return new Result();
    }
    /**以上是提供远程调用的*/

    @GetMapping("/discussList")
    public Result discussList(){
        List<DiscussEntity> list = discussService.list();
        return new Result().ok(list);

    }

    //查询热门话题
    @GetMapping("random")
    public Result random(){
        ListOperations<String, String> stringStringListOperations = redisTemplate.opsForList();
        List<String> range = stringStringListOperations.range(RedisKeys.HOT_DISCUSS, 0, -1);
        return new Result().Viewok(range);
    }

}